package com.pk.platform.business.report.service.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

import com.pk.platform.business.charge.dao.IChargeIndexDao;
import com.pk.platform.business.report.dao.IRefundReportDao;
import com.pk.platform.business.report.service.IRefundReportService;
import com.pk.platform.business.report.vo.RefundReportVO;
import com.pk.platform.domain.charge.ChargeIndex;

public class RefundReportServiceImpl implements IRefundReportService {

	private IRefundReportDao refundReportDao;
	
	private IChargeIndexDao chargeIndexDao;
	
	public InputStream queryRefundReportList(RefundReportVO rrvo) {
		// 将OutputStream转化为InputStream
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		putDataOnOutputStream(out, rrvo);
		return new ByteArrayInputStream(out.toByteArray());
	}
	
	private void putDataOnOutputStream(OutputStream os, RefundReportVO rrvo) {
		WritableWorkbook workbook = null;
		try {
			workbook = Workbook.createWorkbook(os);
			List<Map<String, Object>> reportList = refundReportDao.queryRefundReportList(rrvo);
			List<ChargeIndex> indexList = chargeIndexDao.queryAllChargeIndexList();
			WritableFont wf1 = new WritableFont(WritableFont.TIMES, 13,	WritableFont.BOLD);
			WritableCellFormat wcf1 = new WritableCellFormat(wf1);
			wcf1.setAlignment(Alignment.CENTRE);
			wcf1.setVerticalAlignment(VerticalAlignment.CENTRE);
			wcf1.setWrap(true);
			WritableFont wf2 = new WritableFont(WritableFont.TIMES, 11,	WritableFont.BOLD);
			WritableCellFormat wcf2 = new WritableCellFormat(wf2);
			wcf2.setAlignment(Alignment.CENTRE);
			wcf2.setVerticalAlignment(VerticalAlignment.CENTRE);
			
			WritableSheet sheet = workbook.createSheet("退费报表", 0);
			Label lb = null;
			Map<String, Object> map;
			sheet.mergeCells(0, 0, 10, 1);
			String head = "退费报表";
			int n = 0;
			lb = new jxl.write.Label(0, 0, head,wcf1);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "学童编号",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "学童姓名",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "费用年份",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "一月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "二月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "三月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "四月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "五月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "六月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "七月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "八月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "九月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "十月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "十一月",wcf2);
			sheet.addCell(lb);
			lb = new jxl.write.Label(n++, 2, "十二月",wcf2);
			sheet.addCell(lb);
			for(ChargeIndex ci : indexList)
			{
				lb = new jxl.write.Label(n++, 2, ci.getIndexName(),wcf2);
				sheet.addCell(lb);
			}
			
			for (int i = 0; i < reportList.size(); i++) {
				int x = i + 3;
				int y = 0;
				map = reportList.get(i);
				lb = new Label(y++, x, map.get("childNum")==null?"":map.get("childNum").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("childName")==null?"":map.get("childName").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("year")==null?"":map.get("year").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("January")==null?"":map.get("January").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("February")==null?"":map.get("February").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("March")==null?"":map.get("March").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("April")==null?"":map.get("April").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("May")==null?"":map.get("May").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("June")==null?"":map.get("June").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("July")==null?"":map.get("July").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("August")==null?"":map.get("August").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("September")==null?"":map.get("September").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("October")==null?"":map.get("October").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("November")==null?"":map.get("November").toString());
				sheet.addCell(lb);
				lb = new Label(y++, x, map.get("December")==null?"":map.get("December").toString());
				sheet.addCell(lb);
				for(ChargeIndex ci : indexList)
				{
					lb = new jxl.write.Label(y++, x, map.get(ci.getIndexName())==null?"":map.get(ci.getIndexName()).toString());
					sheet.addCell(lb);
				}
			}
			workbook.write();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally
		{
			try
			{
				workbook.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public IRefundReportDao getRefundReportDao() {
		return refundReportDao;
	}

	public void setRefundReportDao(IRefundReportDao refundReportDao) {
		this.refundReportDao = refundReportDao;
	}

	public IChargeIndexDao getChargeIndexDao() {
		return chargeIndexDao;
	}

	public void setChargeIndexDao(IChargeIndexDao chargeIndexDao) {
		this.chargeIndexDao = chargeIndexDao;
	}

}
